package n1661;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Main {

	private static class Node{
		int x1,x2,h;
		int lcost,rcost;
		public Node() {
			lcost=Integer.MAX_VALUE;
			rcost=Integer.MAX_VALUE;
		}
	}
	
	static Node[] arr=new Node[1002];
	
	static int N,X,Y,MAX;
	
	static void solve()
	{
		Arrays.sort(arr, 0, N, new Comparator<Node>(){
			public int compare(Node o1, Node o2) {
				return o2.h-o1.h;
			}
		});
		
		int min=Integer.MAX_VALUE;
		for(int i=0;i<=N;i++)
		{
			if(i==N){
				min=Y;
			}
			//初始化
			if(arr[i].x1<=X&&arr[i].x2>=X&&Y-arr[i].h<=MAX)
			{
				arr[i].lcost=Y-arr[i].h+X-arr[i].x1;
				arr[i].rcost=Y-arr[i].h+arr[i].x2-X;
				break;
			}
			
			
		}
		
		
		//开始计算
		for(int i=0;i<N;i++)
		{
			if(arr[i].lcost!=Integer.MAX_VALUE)
			{
				boolean lflag=true,rflag=true;
				for(int j=i+1;j<=N;j++)
				{
					if(arr[i].h-arr[j].h>MAX)break;//超过下降最大高度
					
					if(j==N){
						//到达终点
						//System.out.println(i+":"+arr[i].lcost+","+arr[i].rcost+","+arr[i].h);
						if(lflag&&min>arr[i].lcost+arr[i].h)min=arr[i].lcost+arr[i].h;
						if(rflag&&min>arr[i].rcost+arr[i].h)min=arr[i].rcost+arr[i].h;
						break;
					}
					
					if(lflag&&arr[j].h<arr[i].h&&arr[j].x1<=arr[i].x1&&arr[j].x2>=arr[i].x1)
					{
						//update j lcost,rcost
						int tmp=arr[i].lcost+arr[i].h-arr[j].h+arr[i].x1-arr[j].x1;
						if(arr[j].lcost>tmp)arr[j].lcost=tmp;
						
						tmp=arr[i].lcost+arr[i].h-arr[j].h+arr[j].x2-arr[i].x1;
						if(arr[j].rcost>tmp)arr[j].rcost=tmp;
						
						lflag=false;//左边已更新
						
						//System.out.println(j+":"+arr[j].lcost+","+arr[j].rcost);
					}
					
					if(rflag&&arr[j].h<arr[i].h&&arr[j].x1<=arr[i].x2&&arr[j].x2>=arr[i].x2)
					{
//						update j lcost,rcost
						int tmp=arr[i].rcost+arr[i].h-arr[j].h+arr[i].x2-arr[j].x1;
						if(arr[j].lcost>tmp)arr[j].lcost=tmp;
						
						tmp=arr[i].rcost+arr[i].h-arr[j].h+arr[j].x2-arr[i].x2;
						if(arr[j].rcost>tmp)arr[j].rcost=tmp;
						
						rflag=false;//右边已更新
						
						//System.out.println(j+":"+arr[j].lcost+","+arr[j].rcost);
					}
					
					if(lflag==false&&rflag==false){
						break;
					}
				}
			}
		}
		System.out.println(min);
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{

		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String line=bf.readLine();
		int t=Integer.parseInt(line);
		for(int i=0;i<t;i++)
		{

			StringTokenizer st=new StringTokenizer(bf.readLine());
			N=Integer.parseInt(st.nextToken());
			X=Integer.parseInt(st.nextToken());
			Y=Integer.parseInt(st.nextToken());
			MAX=Integer.parseInt(st.nextToken());
			for(int j=0;j<N;j++)
			{
				st=new StringTokenizer(bf.readLine());
				int x1=Integer.parseInt(st.nextToken());
				int x2=Integer.parseInt(st.nextToken());
				int h=Integer.parseInt(st.nextToken());
				Node node=new Node();
				node.x1=x1;
				node.x2=x2;
				node.h=h;
				arr[j]=node;
			}
			Node last=new Node();
			last.h=0;
			last.x1=-20000;
			last.x2=20000;
			arr[N]=last;
			
			solve();
			
		}
	}

}
